<?php

namespace app\admin\controller;

use app\BaseController;
use app\model\ArtExhibition as A;

class ArtExhibition extends BaseController
{


    private $ptitle = 'title/s';
    private $pdesc = 'desc/s';
    private $pimg = 'img/s';
    private $pcontent = 'content/s';
    private $pstatus = 'status/d';
    private $sort = 'sort/d';

    public function index(string $do = '')
    {
        if ($do == 'json') {
            $where = [];
            $kw  = $this->request->param('kw');
            if ($kw) $where[] = ['title','like','%'.$kw.'%'];
            $list = (new A())->listQuery($where);
            $list = $list->toArray();
            return $this->returnMsg($list,code: 0);
        }
        $this->assign([ 'limit'=>10]); 
        return $this->fetch();
    }
    public function bedit(string $do = '')
    {
        $d = $this->only($do ? ['@token'=>'','id/d/ID参数错误','av','af'] : ['@token'=>'','id/d/ID参数错误', $this->ptitle,$this->pdesc,$this->pimg,$this->pcontent,$this->paddress,$this->pstatus,$this->sort]);
        $id = $d['id'];
        $Myobj = A::get("id = $id");
        if(!$Myobj) return $this->returnMsg("数据不存在");
        if ($do == 'up') {
            $value = $d['av'];
            $field = $d['af'];
            if(!in_array($field,['title','desc','img','content','status','sort'])) return $this->returnMsg("参数错误");
            if($field == 'img') $this->only([$this->pimg]);
            if($Myobj->save([$field=>$value])){
                return $this->returnMsg("设置成功", code: 1);
            }
        }else{
            if($Myobj->save($d)){
                return $this->returnMsg("编辑成功", code: 1);
            }
        }
    }
    public function badd()
    {
        $d = $this->only(['@token'=>'id/d/ID参数错误',$this->sort,$this->ptitle,$this->pdesc,$this->pimg,$this->pcontent,$this->pstatus]);
        if (!empty($d["img"])) $d["img"] = $d["img"][0];
        if((new A())->save($d)){
            return $this->returnMsg("添加成功", code: 1);
        }else{
            return $this->returnMsg("添加失败");
        }
    }

    //bdel 删除
    public function bdel()
    {
            $id = $this->only(['@token'=>'','id'])['id'];
            $id = is_array($id) ? implode(',',$id) : $id;
            if(!$id) return $this->returnMsg('参数错误');
            $ids = explode(',', $id);
            if((new A())->where('id','in',$id)->delete()){
                return $this->returnMsg("删除成功", code: 1);
            }else{
                return $this->returnMsg("删除失败");
            }
    }
}